<template>
  <div class="panel">
    <el-container class="full-height">
      <el-header height="60px" class="header no-select">
        <p>我的群组</p>
      </el-header>
      <el-header height="60px" class="no-select subheader">
        <p :class="{ active: tabIndex == 0 }" @click="tabIndex = 0">
          全部群组({{ items.length }})
        </p>
        <el-divider direction="vertical" />
        <p :class="{ active: tabIndex == 1 }" @click="tabIndex = 1">
          我的创建({{ counts[1] }})
        </p>
        <el-divider direction="vertical" />
        <p :class="{ active: tabIndex == 2 }" @click="tabIndex = 2">
          我的管理({{ counts[2] }})
        </p>
        <el-divider direction="vertical" />
        <p :class="{ active: tabIndex == 3 }" @click="tabIndex = 3">
          我的加入({{ counts[3] }})
        </p>
      </el-header>
      <el-main class="panel-body no-padding lum-scrollbar">
        <template v-if="status == 0">
          <Loading />
        </template>
        <template v-else-if="status == 1 && items.length == 0">
          <el-empty description="暂未加入群聊" />
        </template>

        <template v-if="status == 1">
          <div v-for="(item, index) in filterGroups" :key="item.id" class="data-item">
            <el-avatar class="avatar" shape="square" :size="35" :src="item.avatar">
              {{ item.group_name.substr(0, 1) }}
            </el-avatar>
            <div class="card">
              <div class="title">
                <span class="name">{{ item.group_name }}</span>
                <div v-show="item.is_disturb == 1" class="larkc-tag">
                  <i class="fa fa-bell-slash-o" style="font-size: 10px; color: #7d7a7a" />
                </div>
                <div v-show="item.leader == 2" class="larkc-tag wait">群主</div>
              </div>
              <div class="content">[群介绍] ~ 「{{ item.group_profile ? item.group_profile : '未设置' }}」</div>
            </div>
            <div class="apply-from" @click.prevent.stop>
              <el-button size="small" type="primary" :icon="Promotion" @click="toTalkPage(2, item.id)">发送消息</el-button>
              <el-button v-if="item.is_dismiss != 1" size="small" type="danger" :icon="Delete" @click="deleteGroup(item, index)">退出群聊</el-button>
            </div>
          </div>
        </template>
      </el-main>
    </el-container>

    <div class="broadside-box" v-show="groupDetailId">
      <GroupPanel
          :group-id="groupDetailId"
          @close="groupDetailId = 0"
          @send-group="sendMessage"
          @quit-group="quitGroupSuccess"
          @disturb-change="disturbChange"
        />
    </div>
  </div>
</template>

<script>
import { Promotion, Delete } from '@element-plus/icons-vue'
import Loading from '/@/views/chat/components/Loading.vue'
import { onMounted, reactive, toRefs, computed, watch } from 'vue'
import { toTalk } from '/@/views/chat/utils/talk.js'
import { ElMessage, ElMessageBox } from 'element-plus'
import { useChatApi } from '/@/api/chat/index'
import GroupPanel from '/@/views/chat/components/GroupPanel.vue'

export default {
    name: 'PhoneBookGroups',
    components: {
        Loading,
        GroupPanel
    },
    setup() {
        const chatApi = useChatApi()
        const state = reactive({
            tabIndex: 0,
            // 我的群组
            items: [],
            status: 0,
            counts: {
                1: 0,
                2: 0,
                3: 0,
            },
            groupDetailId: 0
        })
        const filterGroups = computed(() => {
            return state.items.filter(item => {
                if (state.tabIndex == 1) {
                return item.leader == 2
                } else if (state.tabIndex == 2) {
                return item.leader == 1
                } else if (state.tabIndex == 3) {
                return item.leader == 0
                }

                return true
            })
        })
         // 加载群聊列表接口
        const loadUserGroups = () => {
            chatApi.ServeGetGroups().then(res => {
                if (res.code == 0) {
                    state.status = 1
                    state.items = res.data.rows
                }
            })
        }
        // 根据用户对话索引获取对话数组对应的key
        const getGroupIndex = (group_id) => {
            return state.items.findIndex(item => item.id == group_id)
        }
        // 退出群聊
        const deleteGroup = (item, index) => {
            ElMessageBox.confirm(
                `您确定要退出【${item.group_name}】群聊吗？`, 
                '温馨提示', 
            {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                showCancelButton: true,
                customClass: 'border-radius0',
                closeOnClickModal: true
            }).then(() => {
                let group_id = item.id
                chatApi.ServeSecedeGroup({
                    group_id,
                }).then(res => {
                    if (res.code == 0) {
                        state.items.splice(index, 1)
                        ElMessage({
                            message: `您已成功退出【${item.group_name}】群聊 ...`,
                            type: 'success',
                        })
                    } else {
                        ElMessage({
                            message: `退出 【${item.group_name}】】群聊失败 ...`,
                            type: 'info',
                        })
                    }
                })
            })
        }
        // 跳转聊天页面
        const toTalkPage = (talk_type, receiver_id) => {
            toTalk(talk_type, receiver_id)
        }
        // 群聊窗口点击发送群聊信息按钮回调事件
        const sendMessage = (group_id) => {
            toTalkPage(2, group_id)
        }
        // 用户退出群聊回调事件
        const quitGroupSuccess = () => {
            state.items.splice(getGroupIndex(state.groupDetailId), 1)
            state.groupDetailId = 0
        }
        // 发起群聊成功后回调方法
        // eslint-disable-next-line no-unused-vars
        const groupChatSuccess = (data) => {
            // state.launchGroupShow = false
            loadUserGroups()
        }
        // 修改群聊免打扰状态
        const disturbChange = (detail) => {
            let index = getGroupIndex(detail.group_id)
            state.items[index].is_disturb = detail.status
        }
        onMounted(() => {
            loadUserGroups()
        })
        watch(() => state.items,
            () => {
                state.items.forEach(item => {
                    if (item.leader == 0) {
                        state.counts[3]++
                    } else if (item.leader == 1) {
                        state.counts[2]++
                    } else if (item.leader == 2) {
                        state.counts[1]++
                    }
                })
            }
        )
        return {
            ...toRefs(state),
            Promotion,
            Delete,
            loadUserGroups,
            getGroupIndex,
            deleteGroup,
            toTalkPage,
            sendMessage,
            quitGroupSuccess,
            groupChatSuccess,
            disturbChange,
            filterGroups
        }
    }
}
</script>

<style lang="scss" scoped>
.border-radius0 {
  border-radius: 0;
}
.aside-box {
  position: relative;
  background-color: white;
  border-right: 1px solid rgb(245, 245, 245);
  overflow: hidden;
  padding: 0;

  .header {
    display: flex;
    flex-direction: row;
    align-items: center;
    padding: 0 15px;

    .from {
      flex: 1 1;
      flex-shrink: 0;
      :deep(.el-input .el-input__inner) {
        border-radius: 20px;
        width: 175px;
      }
    }

    .tools {
      flex-basis: 32px;
      flex-shrink: 0;
      height: 32px;
      cursor: pointer;
      text-align: center;
      position: relative;
      user-select: none;

      .tools-menu {
        position: absolute;
        right: 0;
        top: 38px;
        width: 100px;
        min-height: 80px;
        box-sizing: border-box;
        background-color: rgba(31, 35, 41, 0.9);
        border-radius: 5px;
        z-index: 1;
        padding: 3px 0;

        .menu1-item {
          height: 40px;
          line-height: 40px;
          color: white;
          font-size: 14px;

          &:hover {
            background-color: rgba(70, 72, 73, 0.9);
          }
        }
      }
    }
  }
}
// 右侧面板
.panel {
  position: relative;
  width: 100%;
  height: 100%;
  box-sizing: border-box;

  .header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    &.border{
      border-bottom: 1px solid #f5f5f5;
    }
  }

  .subheader {
    display: flex;
    align-items: center;
    justify-content: flex-start;
    border-top: 1px solid var(--el-border-color-light);
    border-bottom: 1px solid var(--el-border-color-light);
    height: 60px;
    p {
      padding: 0 10px;
      cursor: pointer;
      font-size: 13px;

      &:first-child {
        padding-left: 0;
      }

      &.active {
        color: var(--el-color-primary);
      }
    }
  }

  .panel-body {
    overflow: auto;
    width: 100%;
    height: 100%;
    box-sizing: border-box;

    .preloading {
      height: 100%;
      width: 100%;
      display: flex;
      justify-content: center;
      align-items: center;
      flex-direction: column;
      user-select: none;

      p {
        margin-top: 20px;
        color: #afacac;
        font-size: 14px;
        font-weight: 300;
      }
    }

    .data-item {
      display: flex;
      flex-direction: row;
      align-items: center;
      height: 60px;
      cursor: pointer;
      padding: 5px 15px;
      position: relative;
      overflow: hidden;
      border-bottom: 1px solid #f1ebeb;
      margin-bottom: 2px;

      .avatar {
        height: 35px;
        width: 35px;
        flex-basis: 35px;
        flex-shrink: 0;
        background-color: #508afe;
        border-radius: 50%;
        overflow: hidden;
        display: flex;
        justify-content: center;
        align-items: center;
        font-size: 14px;
        color: white;
        user-select: none;
        transition: ease 1s;
        position: relative;
      }

      .card {
        height: 40px;
        display: flex;
        align-content: center;
        flex-direction: column;
        flex: 1 1;
        margin-left: 10px;
        overflow: hidden;

        .title {
          width: 100%;
          height: 20px;
          display: flex;
          align-items: center;

          .name {
            margin-right: 15px;
            color: #1f2329;
          }

          .larkc-tag {
            font-size: 12px;
            font-weight: 400;
            display: inline-flex;
            align-items: center;
            justify-content: center;
            padding: 0 6px;
            height: 20px;
            border-radius: 2px;
            cursor: default;
            user-select: none;
            background-color: #dee0e3;
            transform: scale(0.8);
            transform-origin: left;
            flex-shrink: 0;
          }

          .wait {
            background: #ffb445;
            color: white;
          }

          .agree {
            background: #53bd53;
            color: white;
          }
        }

        .content {
          font-size: 10px;
          line-height: 18px;
          color: #8f959e;
          overflow: hidden;
          margin-top: 3px;
          font-weight: 300;
          white-space: nowrap;
          text-overflow: ellipsis;
        }
      }

      .apply-from {
        display: flex;
        justify-content: center;
        align-items: center;
        flex-direction: column;
        position: relative;
        right: -110px;
        top: 0px;
        height: 60px;
        width: 100px;
        transition: ease 0.5s 0.3s;
        background-color: white;
        opacity: 0;
        button {
          margin: 2px;
        }
      }

      &:hover {
        box-shadow: 0 0 8px 4px #f1f1f1;

        .avatar {
          border-radius: 2px;
        }

        .apply-from {
          opacity: 1;
          right: 0px;
        }
      }
    }
  }
}
.broadside-box {
  position: absolute;
  width: 350px;
  height: 100%;
  top: 0;
  right: 0;
  z-index: 2;
  animation: showBox 0.5s ease-in-out;
  -webkit-animation: showBox 0.5s ease-in-out;
  -moz-animation: showBox 0.5s ease-in-out;
  -webkit-box-direction: normal;
  background: white;
  box-shadow: 0 0 14px #cccccc70;
}
@keyframes showBox {
  0% {
    transform: translateX(350px);
  }

  to {
    transform: translateX(0);
  }
}
@-webkit-keyframes showBox {
  0% {
    -webkit-transform: translateX(350px);
  }

  to {
    -webkit-transform: translateX(0);
  }
}
</style>